package com.servlet;

import com.alibaba.fastjson.JSON;
import com.pojo.user;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(value = "/user_Login")
public class user_Login extends HttpServlet {
    userService us = new userService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        user user = JSON.parseObject(s, user.class);

        String account = user.getUserAccount();
        String password =  user.getUserPassword();
        System.out.println("登录的账号密码是"+account+" "+password);

        user u = (user)us.userlogin(account, password);

        if(u!=null)
        {
            //设置session值，展示xxx欢迎你
            HttpSession session = req.getSession();
            session.setAttribute("user",u);


            res.getWriter().write("yes");

            System.out.println("ok!登录成功");
        }else{
            res.getWriter().write("no");
            System.out.println("no");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
